"""
serializers
Administrator
2023/12/6
convergence-server
"""
import re
from rest_framework import serializers
from utils.response_util import APIResponse
from company.models import Company,Department
from django.contrib.sites.shortcuts import get_current_site
from rest_framework.fields import SerializerMethodField

class CompanySerializer(serializers.ModelSerializer):
    """公司管理"""
    # menu_childs = PermissionSerializer(many=True, read_only=True)
    image_license_url = SerializerMethodField()
    image_corporation_back_url = SerializerMethodField()
    image_corporation_front_url = SerializerMethodField()

    class Meta:
        model = Company
        fields = ('id', 'name', 'license_number', 'is_auth', 'id_card','image_license_url','license_url',
                  'mobile','corporation','image_id','corporation_front_url','corporation_back_url',
                  'image_corporation_back_url','image_corporation_front_url',
                  'corporation_front_image_id','corporation_back_image_id')

    def get_image_license_url(self, obj):
        current_site = get_current_site(self.context.get('request'))
        domain_name = current_site.domain
        return f"http://{domain_name}/{obj.license_url}"

    def get_image_corporation_front_url(self, obj):
        current_site = get_current_site(self.context.get('request'))
        domain_name = current_site.domain
        return f"http://{domain_name}/{obj.corporation_front_url}"


    def get_image_corporation_back_url(self, obj):
        current_site = get_current_site(self.context.get('request'))
        domain_name = current_site.domain
        return f"http://{domain_name}/{obj.corporation_back_url}"
    def create(self, validated_data):
        exist = Company.objects.filter(name=validated_data['name']).exists()
        if exist:
            return APIResponse(code=1000, msg='公司已存在')
        else:
            # 消除域名
            if validated_data['license_url']:
                current_site = get_current_site(self.context.get('request'))
                domain_name = current_site.domain
                validated_data['license_url'] = validated_data['license_url'].replace('http://' + str(domain_name), '')
                validated_data['license_url'] = validated_data['license_url'].replace('https://' + str(domain_name), '')

        instance = Company.objects.create(**validated_data)
        return instance

    def update(self, instance, validated_data):

        exist = Company.objects.filter(name=validated_data['name']).values()
        if len(exist) > 1:
            return APIResponse(code=1000, msg='公司已存在')
        else:
            # 消除域名
            current_site = get_current_site(self.context.get('request'))
            domain_name = current_site.domain
            validated_data['license_url'] = validated_data['license_url'].replace('http://' + str(domain_name), '')
            validated_data['license_url'] = validated_data['license_url'].replace('https://' + str(domain_name), '')

            Company.objects.filter(pk=instance.pk).update(**validated_data)
        return instance



class DepartmentSerializer(serializers.ModelSerializer):
    """管理"""

    company_name = serializers.SerializerMethodField()
    class Meta:
        model = Department
        fields = "__all__"


    def get_company_name(self,obj):
        return obj.company.name


    def create(self, validated_data):
        exist = Department.objects.filter(name=validated_data['name'],company_id=validated_data['company']).exists()
        if exist:
            return APIResponse(code=1000, msg='部门已存在')

        instance = Department.objects.create(**validated_data)
        return instance

    def update(self, instance, validated_data):
        exist = Department.objects.filter(name=validated_data['name'],company_id=validated_data['company']).values()
        if len(exist) > 0:
            return APIResponse(code=1000, msg='部门已存在')

        Department.objects.filter(pk=instance.pk).update(**validated_data)
        return instance

